package org.eiahe.hr.performance.controller;

import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.eiahe.hr.performance.domain.PerformanceScore;
import org.eiahe.hr.performance.domain.bo.PerformanceScoreBo;
import org.eiahe.hr.performance.domain.vo.PerformanceScoreVo;
import org.eiahe.hr.performance.service.IPerformanceScoreService;
import org.ehe.common.core.domain.R;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.annotation.SaCheckPermission;


import java.util.List;

/**
 * 绩效评分Controller
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/performance/score")
public class PerformanceScoreController extends BaseController {

    private final IPerformanceScoreService performanceScoreService;
    /**
     * 查询绩效评分列表
     */
    @SaCheckPermission("performance:score:list")
    @GetMapping("/list")
    public TableDataInfo<PerformanceScoreVo> list(PerformanceScoreBo score, PageQuery pageQuery) {
        return performanceScoreService.queryPageListVo(score, pageQuery);
    }

    /**
     * 获取绩效评分详细信息
     */
    @SaCheckPermission("performance:score:query")
    @GetMapping(value = "/{scoreId}")
    public R<PerformanceScoreVo> getInfo(@PathVariable("scoreId") Long scoreId) {
        return R.ok(performanceScoreService.queryById(scoreId));
    }

    /**
     * 新增绩效评分
     */
    @SaCheckPermission("performance:score:add")
    @Log(title = "绩效评分", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody PerformanceScoreBo scoreBo) {
        // 如果提供了评分人列表，则批量创建评分任务
        if (scoreBo.getScorers() != null && !scoreBo.getScorers().isEmpty()) {
            return toAjax(performanceScoreService.insertBatchByBo(scoreBo));
        } else {
            // 否则按原来的逻辑单个创建
            return toAjax(performanceScoreService.insertByBo(scoreBo));
        }
    }

    /**
     * 修改绩效评分
     */
    @SaCheckPermission("performance:score:edit")
    @Log(title = "绩效评分", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody PerformanceScoreBo scoreBo) {
        // 如果提供了评分人列表，则使用批量更新
        if (scoreBo.getScorers() != null && !scoreBo.getScorers().isEmpty()) {
            return toAjax(performanceScoreService.updateBatchByBo(scoreBo));
        } else {
            // 否则按原来的逻辑单个更新
            return toAjax(performanceScoreService.updateByBo(scoreBo));
        }
    }

    /**
     * 删除绩效评分
     */
    @SaCheckPermission("performance:score:remove")
    @Log(title = "绩效评分", businessType = BusinessType.DELETE)
    @DeleteMapping("/{scoreIds}")
    public R<Void> remove(@PathVariable Long[] scoreIds) {
        return toAjax(performanceScoreService.deleteWithValidByIds(List.of(scoreIds), true));
    }

    /**
     * 发布绩效考核计划
     */
    @SaCheckPermission("performance:score:publish")
    @Log(title = "绩效考核计划发布", businessType = BusinessType.INSERT)
    @PostMapping("/publish/{scoreId}")
    public R<Void> publish(@PathVariable Long scoreId) {
        return toAjax(performanceScoreService.publish(scoreId));
    }



}
